package com.bosssoft.ct.springcloud.controller;

import com.bosssoft.ct.springcloud.pojo.dto.UserDTO;
import com.bosssoft.ct.springcloud.pojo.query.UserQuery;
import com.bosssoft.ct.springcloud.service.UserService;
import com.bosssoft.ct.springcloud.utils.http.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户管理逻辑控制器
 * @author ChenTong
 * @version 1.0
 * @date 2020/6/21 20:18
 * @modified
 **/
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @DeleteMapping("/{id}")
    public HttpResult delete(@PathVariable("id") Long id){

        int result =  userService.delete(id);
        if (result == 1){
            return HttpResult.ok("删除成功");
        }
        return HttpResult.error("删除失败");
    }

    @GetMapping
    public HttpResult getUsers(UserQuery query){
        log.info("getUsers: params{}", query);
        return HttpResult.ok(userService.selectByCondition(query));
    }

    @PutMapping
    public HttpResult update(@RequestBody UserDTO userDTO){
        int result =  userService.update(userDTO);
        if (result == 1){
            return HttpResult.ok("更新成功");
        }
        return HttpResult.error("更新失败");
    }

    @PostMapping
    public HttpResult insert(@RequestBody UserDTO userDTO){
        log.info("insert:{}",userDTO);
        int result =  userService.insert(userDTO);
        if (result == 1){
            return HttpResult.ok("更新成功");
        }
        return HttpResult.error("更新失败");
    }
}
